package com.bizihang.validate.code.core.support;

import com.bizihang.validate.code.core.Client;
import com.bizihang.validate.code.core.ValidateCode;
import com.bizihang.validate.code.core.ValidateCodeRepository;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 默认的仓库实现,底层采用{@code java.util.concurrent.ConcurrentHashMap}
 */
public class DefaultValidateCodeRepository implements ValidateCodeRepository {

    private Map<String, ValidateCode> mapRepository = new ConcurrentHashMap<>();

    @Override
    public void save(Client client, ValidateCode validateCode) {
        mapRepository.put(client.getClientId(), validateCode);
    }

    @Override
    public Optional<ValidateCode> get(Client client) {
        return Optional.ofNullable(mapRepository.get(client.getClientId()));
    }

    @Override
    public void remove(Client client) {
        mapRepository.remove(client.getClientId());
    }
}
